package leetcode.editor.cn;

public class _10_RegularExpressionMatching {
    public static void main(String[] args) {
        Solution solution = new _10_RegularExpressionMatching().new Solution();
        boolean flag = solution.isMatch("aaa", ".*");
        System.out.println(flag);
    }

    //leetcode submit region begin(Prohibit modification and deletion)

    class Solution {
        public boolean isMatch(String s, String p) {
            int n1 = s.length(), n2 = p.length();
            boolean[][] dp = new boolean[n1 + 1][n2 + 1];

            // base case
            dp[0][0] = true;
            for (int j = 1; j <= n2; j++) {
                if (p.charAt(j - 1) == '*') {
                    dp[0][j] = dp[0][j - 2];
                }
            }

            // dp
            for (int i = 1; i <= n1; i++) {
                for (int j = 1; j <= n2; j++) {
                    if (p.charAt(j - 1) == '.') {
                        dp[i][j] = dp[i - 1][j - 1];
                    } else if (p.charAt(j - 1) == '*') {
                        if (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') { // 判断 '*' 前面一个字符
                            // 相等则可取可不取
                            dp[i][j] = dp[i - 1][j] || dp[i][j - 2];
                        } else {
                            // 不相等只能不取
                            dp[i][j] = dp[i][j - 2];
                        }
                    } else {
                        dp[i][j] = dp[i - 1][j - 1] && s.charAt(i - 1) == p.charAt(j - 1);
                    }
                }
            }
            return dp[n1][n2];
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}